package xml;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Iterator;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

import dataStructures.*;

@SuppressWarnings("restriction")
public class QueryReader {
	@SuppressWarnings({ "unchecked" })
	public MyQuery readXML(String filename) {
		try {
			// First create a new XMLInputFactory
			XMLInputFactory inputFactory = XMLInputFactory.newInstance();
			// Setup a new eventReader
			InputStream in = new FileInputStream(filename);
			XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
			// Read the XML document
			MyQuery query = new MyQuery();
			Location location = null;
			
			while (eventReader.hasNext()) {
				XMLEvent event = eventReader.nextEvent();

				if (event.isStartElement()) {
					StartElement startElement = event.asStartElement();
					// If we have a item element we create a new item
					if (startElement.getName().getLocalPart() == (Const.QUERY)) {
						
						// We read the attributes from this tag and add the date attribute to our object
						Iterator<Attribute> attributes = startElement.getAttributes();
						while (attributes.hasNext()) {
							Attribute attribute = attributes.next();
							if (attribute.getName().toString().equals(Const.TERM));
							query.term = attribute.getValue();
						}
					}

					if (event.isStartElement()) {
						if (event.asStartElement().getName().getLocalPart().equals(Const.LOCATION)) {
							location = new Location();
							
							Iterator<Attribute> attributes = startElement.getAttributes();
							while (attributes.hasNext()) {
								Attribute attribute = attributes.next();
								if (attribute.getName().toString().equals(Const.LATITUDE))
									location.latitude = attribute.getValue();
								if (attribute.getName().toString().equals(Const.LONGITUDE))
									location.longitude = attribute.getValue();
								if (attribute.getName().toString().equals(Const.RADIUS))
									location.radius = attribute.getValue();
								if (attribute.getName().toString().equals(Const.UNIT))
									location.unit = attribute.getValue();
							}
						}
					}
				}
				// If we reach the end of an item element we add it to the list
				if (event.isEndElement()){
					EndElement endElement = event.asEndElement();
					if (endElement.getName().getLocalPart() == (Const.QUERY)){
						return query;
					}
					if (endElement.getName().getLocalPart() == (Const.LOCATION)){
						query.location = location;
					}
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (XMLStreamException e) {
			e.printStackTrace();
		}
		return null; 
	}

}